﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;
using JKF.DataModule.IDAL;
using JKF.Substructure;//请先添加引用
namespace JKF.DataModule.SqlServerDAL
{
    public partial class dalPOINT_EXCHANGE : IdalPOINT_EXCHANGE
    {
        private const string FieldShow = "[EXCHANGE_KEY],[EXCHANGE_NO],[CUSTOMER_KEY],[GIFTS],[DEDUCT_POINT],DATE_EXCHANGE,[STATUS],[MEMO],convert(char(10),[DATE_CREATE],120) DATE_CREATE,[CARD_NO],[NAME],[MOBILE],[ACCOUNT],[REAL_NAME]";
        private const string FieldOrder = "DATE_EXCHANGE desc";
        private const string FieldKey = "EXCHANGE_KEY";

        public  DataTable GetPointExchange(string searchItem,string matchWay,string keyword, string dateStart, string dateEnd,
         string ownShop, string userKey, int pageCurrent, int pageSize, ref int pageCount, ref int rowCount)
        {
            var sql = new StringBuilder(@"  1=1 and STATUS=1 ");

            sql.AppendFormat(" and exists(select shop_code from sys_user_shop  where V_POINT_EXCHANGE.shop_code=sys_user_shop.shop_code  and sys_user_shop.USER_KEY='{0}')", userKey);

            if (!string.IsNullOrEmpty(ownShop))
            {
                sql.AppendFormat(" and Shop_code in('{0}')", string.Join("','", ownShop.Split(',')));
            }

            if (!string.IsNullOrEmpty(searchItem) && !string.IsNullOrEmpty(keyword))
            {
                if (matchWay == "包含")
                {
                    sql.AppendFormat(" AND {0} LIKE '%{1}%' ", searchItem,keyword);
                }
                else
                {
                   sql.AppendFormat(" AND {0} = '{1}' ", searchItem,keyword);
                }
            }
            if (!string.IsNullOrEmpty(dateStart))
            {
                sql.AppendFormat(" and DATE_CREATE >'{0}'", dateStart);
            }
            if (!string.IsNullOrEmpty(dateEnd))
            {
                sql.AppendFormat(" and DATE_CREATE <'{0}'", dateEnd);
            }

            return DataPage.GetPageInfo("V_POINT_EXCHANGE", FieldKey, pageCurrent, pageSize, FieldShow, FieldOrder, sql.ToString(), ref pageCount, ref rowCount).Tables[0];
        }


    }
}
